﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerOne : MonoBehaviour
{
    public float maxSpeedH;
    public float maxSpeedV;

    public float curSpeedH;
    public float curSpeedV;

    //子弹参数
    public int attackCombo = 1;             //连发几个
    public float attackComboCd = 0.15f;     //连发间隔
    public int attackNum = 1;               //散射几个
    public float attackCd = 0.6f;           //攻击间隔
    public float attackSpeed = 2f;          //子弹飞行速度
    public float attackAngle = 0f;          //出生角度
    public int attackDmg;                   //攻击力

    public GameObject playerOneProject;
    public GameObject spawnPointObj;
    public GameObject SpawnHelper;

    public AudioClip AudioClipAttack;
    public AudioClip AudioClipDie;

    private Rigidbody2D _rb2d;
    private Collider2D _col2d;
    private Animator _anim;
    private AudioSource _audioSource;
    private Vector2 _input;
    private float _attackTimeStamp;
    private bool bIsDead = false;

    void Awake()
    {
        _rb2d = this.GetComponent<Rigidbody2D>();
        _col2d = this.GetComponent<Collider2D>();
        _anim = this.GetComponent<Animator>();
        _audioSource = this.GetComponent<AudioSource>();
    }

    private void OnEnable()
    {
        EventMgr.Register(Events.ON_PLAYER1_INPUT, OnPlayer1Input);
        EventMgr.Register(Events.ON_PLAYER1_ATTACK, OnPlayer1Attack);
    }

    private void OnDisable()
    {
        EventMgr.UnRegister(Events.ON_PLAYER1_INPUT, OnPlayer1Input);
        EventMgr.UnRegister(Events.ON_PLAYER1_ATTACK, OnPlayer1Attack);
    }

    private void OnPlayer1Input(object arg1, EventArgs arg2)
    {
        if (bIsDead)
        {
            curSpeedH = 0f;
            curSpeedV = 0f;
            return;
        }

        _input = (Vector2)arg1;

        curSpeedH = Mathf.Clamp(_input.x * maxSpeedH, -maxSpeedH, maxSpeedH);
        curSpeedV = Mathf.Clamp(_input.y * maxSpeedV, -maxSpeedV, maxSpeedV);
    }

    private void OnPlayer1Attack(object arg1, EventArgs arg2)
    {
        //check attack cd
        if (bIsDead)
        {
            return;
        }
        if (Time.time < _attackTimeStamp + attackCd)
        {
            return;
        }

        _anim.SetTrigger("Attack");

        _audioSource.clip = AudioClipAttack;
        _audioSource.Play();

        _attackTimeStamp = Time.time;
        GameObject SpawnHelperObj = GameObject.Instantiate(SpawnHelper, this.transform);
        SpawnHelperObj.GetComponent<PlayerOneSpawnManager>().Init(this, attackCombo, attackComboCd, attackNum, attackCd, attackSpeed, attackAngle, attackDmg);
    }

    void FixedUpdate()
    {
        if (bIsDead)
        {
            return;
        }
        Vector2 pos = _rb2d.position;
        float x = Mathf.Clamp(pos.x + curSpeedH * Time.fixedDeltaTime, EnumBase.playerMinX, EnumBase.playerMaxX);
        float y = Mathf.Clamp(pos.y + curSpeedV * Time.fixedDeltaTime, EnumBase.playerMinY, EnumBase.playerMaxY);
        pos = new Vector2(x, y);
        _rb2d.MovePosition(pos);
    }

    public void SpawnProject(Vector2 velocity, int dmg)
    {
        GameObject __project = GameObject.Instantiate(playerOneProject, spawnPointObj.transform.position, Quaternion.Euler(velocity.x, velocity.y, 0f));
        //设置子弹
        __project.GetComponent<PlayerOneProject>().InitProject(velocity, dmg);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (bIsDead)
        {
            return;
        }
        if (collision.transform.CompareTag("Player2Project"))
        {
            Kill();
        }
        else if (collision.transform.CompareTag("EnemyProject"))
        {
            Kill();
        }
        else if (collision.transform.CompareTag("Enemy"))
        {
            Kill();
        }
    }

    public void Kill()
    {
        if (bIsDead)
        {
            return;
        }
        if (PlayerController.Instance.bIsGodMode)
        {
            return;
        }

        bIsDead = true;
        _anim.SetTrigger("Die");

        _audioSource.clip = AudioClipDie;
        _audioSource.Play();

        EventMgr.Call(Events.ON_PLAYER1_DIE);
    }
}
